package com.tea.servlet;

import com.tea.dao.ProductDao;
import com.tea.entity.Product;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/dashboard")
public class AdminDashboardServlet extends HttpServlet {
    private final ProductDao productDao = new ProductDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 获取所有商品数据
            List<Product> products = productDao.getAll();
            // 将商品数据设置到request属性中
            request.setAttribute("products", products);
            // 转发到admin-dashboard.jsp页面
            request.getRequestDispatcher("/admin-dashboard.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", "获取商品数据失败");
            request.getRequestDispatcher("/admin-dashboard.jsp").forward(request, response);
        }
    }
}